@push('title')
    <title>特政-订单列表</title>
@endpush
@push('style2')
    <style>


        .layui-table-cell{
            height: auto;
            overflow:visible;
            text-overflow:inherit;
            white-space:normal;
            word-break: break-all;
        }
    </style>
@endpush
@extends('layout.layer')
@section('content')

    <form class="layui-form layui-form-pane1" action="" lay-filter="first" style="margin-bottom: 15px">

        {{--@if(in_array(10,request()->admin_platform) && in_array(11,request()->admin_platform))

            <div class="layui-form-item">
                <label class="layui-form-label">所上平台</label>
                <div class="layui-input-inline">
                    <select name="platform">
                        <option value="10">恒博携程</option>
                        <option value="11">茂华携程</option>
                    </select>
                </div>
            </div>
        @elseif(in_array(10,request()->admin_platform))
            <input type="hidden" name="platform" value="10">
        @elseif(in_array(11,request()->admin_platform))
            <input type="hidden" name="platform" value="11">
        @endif

        <div class="layui-input-inline">
            <label class="layui-form-label">政策类型</label>
            <div class="layui-input-inline">
                <select name="type" lay-filter="interest">
                    <option value="">所有</option>
                    <option value="1">特殊政策</option>
                    <option value="2">运价直连</option>
                </select>
            </div>
        </div>

        <div class="layui-input-inline">
            <label class="layui-form-label">数据源</label>
            <div class="layui-input-inline">
                <select name="status" lay-filter="interest">
                    <option value="0">所有</option>
                    <option value="1">蜗牛数据</option>
                </select>
            </div>
        </div>

        <div class="layui-input-inline">
            <label class="layui-form-label">政策代码</label>
            <div class="layui-input-inline">
                <input type="text" name="policy_code" maxlength="" placeholder="" autocomplete="off" class="layui-input" >
            </div>
        </div>

        <div class="layui-input-inline">
            <label class="layui-form-label">航司</label>
            <div class="layui-input-inline">
                <input type="text" name="airline#like" maxlength="" placeholder="" autocomplete="off" class="layui-input" >
            </div>
        </div>--}}

        <div class="layui-input-inline">
            <button class="layui-btn" id="search-btn" lay-submit lay-filter="*">搜索</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>

    </form>
    <table id="test2" lay-filter="test2"></table>
    <script type="text/html" id="switch-seat">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="switch-seat">换位</a>
    </script>
    <script type="text/html" id="result-option">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="issue-ticket">出票</a>
    </script>

@endsection
@push('script')
    <script>

        layui.use(['table','form','laydate'], function() {
            var $ = layui.$
                ,table = layui.table
                ,form = layui.form
                ,laydate = layui.laydate;


            //日期
            laydate.render({
                elem: '#day'
                ,type: 'date'
            });


            var tableData = {};

            table.render({
                id: 'test2'
                ,elem: '#test2'
                ,url: '/javaapi/seat/policyOrder/pageList?policyCode=QWFAN'
                ,page: { //详细参数可参考 laypage 组件文档
                    curr: 1
                    ,groups: 20
                    ,first: 1
                    ,last: true
                    ,layout: ['limit', 'prev', 'page', 'next', 'count','refresh', 'skip'] //自定义分页布局
                }
                //,height: 300
                ,cellMinWidth: 80
                ,limit : 30
                //,skin: 'line'
                ,toolbar: false
                ,parseData: function (res) { //将原始数据解析成 table 组件所规定的数据

                    if(res.data.records.length > 0) {

                        var occupyEtermArray = [];

                        $.ajax({
                            url: '/javaapi/eterm/account/api/getAccountList'
                            , method: 'post'
                            , contentType: "application/json" // 发送信息至服务器时内容编码类型
                            , dataType: 'json'
                            , async: false
                            , success: function (etermData) {

                                if(etermData.code === 0){

                                    occupyEtermArray = etermData.data;
                                }

                            },
                        });

                        // 表格数据渲染完毕后的回调
                        // 假设我们要在第二行后面添加一行
                        res.data.records.forEach(function (item, index) {

                            res.data.records[index].occupyEtermArray = occupyEtermArray;
                        });
                    }



                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data":res.data.records,
                    };


                }
                ,request: {
                    pageName: 'pageIndex' //页码的参数名称，默认：page
                    ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
                }
                ,scrollCollapse: false // 禁用列的滑动
                ,cols: [[
                    {type: 'checkbox'}
                    ,{field: 'name', 'title':'平台',templet:function (row) {

                            var platform = '';

                            switch (row.platform) {
                                case 10: platform += '<img src="/static/images/platform/xiecheng.bmp" style="width: 80px;height: auto;"><br><span style="text-align: center">恒博携程</span>';break;
                                case 11: platform += '<img src="/static/images/platform/xiecheng.bmp" style="width: 80px;height: auto;"><br><span style="text-align: center">茂华携程</span>';break;
                                case 20: platform += '<img src="/static/images/platform/xiecheng.bmp" style="width: 80px;height: auto;"><br><span style="text-align: center">恒博规则运价</span>';break;
                                case 21: platform += '<img src="/static/images/platform/xiecheng.bmp" style="width: 80px;height: auto;"><br><span style="text-align: center">茂华规则运价</span>';break;
                                case 30: platform += '<img src="/static/images/platform/qunar.bmp" style="width: 80px;height: auto;"><br><span style="text-align: center">去哪儿</span>';break;
                                case 40: platform += '<img src="/static/images/platform/tongcheng.bmp" style="width: 80px;height: auto;"><br><span style="text-align: center">同程</span>';break;
                            }

                            return platform;
                        }}
                    ,{field: 'policy_code', 'title':'单号/代码',templet:function (row) {
                            return row.orderNo + '<br/>'+row.policyCode;
                        }}
                    ,{field: 'airline', 'title':'航司/航段/航班/舱位',width: 200,templet:function (row) {

                            return row.airline+'/'+row.departCityCode+row.arrivedCityCode+'/'+row.flightCode+'/'+row.cabin;
                        }}
                    ,{field: 'orderAtStr', 'title':'日期',width: 110}
                    ,{field: 'seat', 'title':'占位舱位/总数',width: 120,templet:function (row) {
                            var orderIds = '';
                            if (row.bookOrders.length > 0) {
                                $.each(row.bookOrders,function (index,item) {
                                    orderIds += '<input name="occupyCabin" type="radio" value="'+item.cabin+'">'+item.cabin+'/'+item.travellerNum+'<br/>';
                                })
                            }


                            return orderIds;
                        }}
                    ,{field: 'share_flight_code', 'title':'编码系统',width: 120,templet:function (row) {

                        var orderIds = '';
                        if (row.bookOrders.length > 0) {
                            $.each(row.bookOrders,function (index,item) {
                                orderIds += item.etermUserNo+'<br/>';
                            })
                        }


                        return orderIds;
                        }}
                    ,{field: 'share_flight_code', 'title':'抢位系统',width: 180,templet: function (row) {
                            var occupyId = '';

                            console.log(row)

                            if (row.occupyEtermArray.length > 0) {
                                $.each(row.occupyEtermArray,function (index,item) {
                                    occupyId += '<input name="occupy" type="radio" value="'+item.userNo+'">'+item.userNo+'<span style="color: red">-</span>'+item.limitNum+'/'+item.remainTimes +'<br/>';
                                })
                            }

                            return occupyId;
                        }}

                    ,{field: 'issueCabin', 'title':'确定舱位',width: 120,edit:true,templet:function (row) {

                            return row.issueCabin != null ? row.issueCabin : row.cabin;
                        }}

                    ,{field: 'share_flight_code', 'title':'客数',width: 120,templet:function (row) {

                            var people = '';
                            $.each(row.users,function (index,item) {

                                people += '<input name="users" type="checkbox" value="'+item.id+'" data-name="'+item.name+'" data-cartno="'+item.certNo+'" data-carttype="'+item.certType+'">'+item.name+'<br/>';
                            })

                            return people;
                        }}
                    ,{field: 'share_flight_code', 'title':'开始换位',width: 90,toolbar: '#switch-seat'}
                    ,{field: 'pnr', 'title':'PNR',width: 90}
                    ,{field:'option', title:'操作',toolbar: '#result-option'}
                ]]

            });

            //监听提交
            form.on('submit(*)', function(data){

                table.reload('test2',{
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where:data.field
                });

                return false;
            });

            //监听工具条
            table.on('tool(test2)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    ,layEvent = obj.event; //获得 lay-event 对应的值


                if (layEvent === 'issue-ticket') {

                    $.ajax({
                        url: '/policyupload/ajaxDelete'
                        , method: 'delete'
                        , data: {id:data.id,platform:data.platform,PolicyID:data.policy_id,_token:"{{csrf_token()}}"}
                        , dataType: 'json'
                        , success: function (data) {

                            layer.msg(data.errmsg, {
                                offset: '15px'
                                , icon: data.errcode == 0 ? 1 : 15
                                , time: 3000
                            }, function () {

                            });

                            if(data.errcode == 0) {

                                table.reload("test2")
                            }

                        },
                    });
                } else if(layEvent === 'switch-seat'){


                    var orderIds = [];
                    $('input[name=system]:checked').each(function() {
                        orderIds.push($(this).val());
                    });

                    var occupyIds = [];
                    $('input[name=occupy]:checked').each(function() {
                        occupyIds.push($(this).val());
                    });

                    var userIds = [];
                    $('input[name=users]:checked').each(function() {

                        userIds.push({name:$(this).data('name'),cartNo:$(this).data('cartno'),cartType:$(this).data('carttype')});
                    });

                    if (userIds.length === 0) {

                        layer.msg('请选择需要换位乘客', {
                            offset: '15px'
                            , icon: 15
                            , time: 3000
                        }, function () {

                        });

                        return ;
                    }



                    layer.confirm('确定换位', {
                            title: "确认",
                            btn: ['放抢', '生编','取消']
                        } ,
                        function(index){

                            var occupyEterm =  $('input[name=occupy]:checked').val();

                           /* $("input:checkbox[name='occupy']:checked").each(function () {

                                occupyEterm = $(this).val();
                            });*/

                            var occupyCabin = $('input[name=occupyCabin]:checked').val();

                            /*$("input:checkbox[name='occupyCabin']:checked").each(function () {

                                occupyCabin = $(this).val();
                            });*/


                            var param = {bookPolicyOrderId:data.id,departCityCode:data.departCityCode,arrivedCityCode:data.arrivedCityCode,flightCode:data.flightCode,departAt:data.departAtStr,userIds:userIds,cabin:occupyCabin,etermUserNoFQ:occupyEterm,flag:1}
                            createPnr(param);
                        },
                        function (index) {

                            var param = {bookPolicyOrderId:data.id,departCityCode:data.departCityCode,arrivedCityCode:data.arrivedCityCode,flightCode:data.flightCode,departAt:data.departAt,userIds:userIds,cabin:data.cabin,flag:1}
                            createPnr(param);
                        },
                        function(){

                        }
                    );


                }

            });


            var createPnr = function (param) {

                $.ajax({
                    url: '/javaapi/seat/order/releaseGenerate'
                    , method: 'post'
                    , data: JSON.stringify(param)
                    , contentType: "application/json" // 发送信息至服务器时内容编码类型
                    , dataType: 'json'
                    , success: function (data) {

                        layer.msg(data.msg, {
                            offset: '15px'
                            , icon: data.code === 0 ? 1 : 15
                            , time: 3000
                        }, function () {

                        });

                        if(data.code === 0) {

                            table.reload("test2")
                        }

                    },
                });

            }


            /**
             * 编辑表格
             */
            table.on('edit(test2)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data;

                var param = {id: data.id, issueCabin: data.issueCabin};

                $.ajax({
                    url: '/javaapi/seat/policyOrder/update'
                    , method: 'put'
                    , data: JSON.stringify(param)
                    , contentType: "application/json" // 发送信息至服务器时内容编码类型
                    , dataType: 'json'
                    , success: function (res) {

                        layer.msg(res.msg, {
                            offset: '15px'
                            , icon: res.code === 0 ? 1 : 15
                            , time: 2000
                        }, function () {

                        });

                        if(res.code === 0) {

                            table.reload("test2")
                        }

                    },
                });

            });

        });
    </script>

@endpush